program /s10/banf_main.

class eban_short definition inheriting from /s10/any.

  public section.

* project path
    constants:
       projectpath type string value 'sap/bc/bsp/s10/banf_demo'.

* table fields for list view, plus key fields
    data:
      banfn type eban-banfn, " Bestellanforderung
      bnfpo type eban-bnfpo, " Banf-Position
      statu type eban-statu, " Bearbeitungsstatus
      matnr type eban-matnr, " Material
      menge type eban-menge, " Anforderungsmenge
      preis type eban-preis, " Verrechnungspreis
      meins type eban-meins, " Mengeneinheit using Text
      rlwrt type eban-rlwrt, " Gesamtwert bei Freigabe
      waers type eban-waers, " Währung
      badat type eban-badat, " Anforderungsdatum
      txz01 type eban-txz01, " Kurztext
      afnam type eban-afnam, " Bestellt für
      ernam type eban-ernam, " Angelegt durch
      frgzu type eban-frgzu, " Freigabezustand
      frgst type eban-frgst. " Freigabestrategie


    data:
  s10detailview     type string.

    data: button_release_html type string.

    methods:
      build_button_release_html
        importing
          frgzu               type eban-frgzu
        exporting
          button_release_html type string.


    methods:
      s10standardname redefinition.


* database table name
    constants:
      dbtablename type string value 'eban', " for select

*  assign unit attributes
      unit_menge  type string value 'MEINS'. "Anforderungsmenge

endclass.


class eban_short implementation.

  method build_button_release_html.

    if frgzu = 'X'.
      button_release_html =  |<button style='background-color:orange;height:20px; width:100px;' type='button' class='button' onclick='S10Apply("reset_release")'>| &&
                                     s10localize( 'BANF_DO_RELEASE_RESET' ) && |</button>|.
    else.
      button_release_html =  |<button style='background-color:greenyellow; height:20px; width:100px;' type='button' class='button' onclick='S10Apply("release")'>| &&
         s10localize( 'BANF_DO_RELEASE' ) && |</button>|.
    endif.

  endmethod.


* redefinition of s10standardname() for certain attributes
  method s10standardname.

    case to_lower( attrname ).
      when 'afnam'.

        data: afnam_upper type eban-afnam.
        afnam_upper = to_upper( afnam ).

        if afnam_upper is initial.
          afnam_upper = to_upper( ernam ).
        endif.

* read user name

        data: usr type usr03.

        call function 'SUSR_USER_ADDRESS_READ'
          exporting
            user_name  = afnam_upper
          importing
            user_usr03 = usr
          exceptions
            others     = 1.

        if usr-name2 is not initial.
          concatenate usr-salut usr-name1 usr-name2  into text separated by space.
          condense text.

          if usr-abtlg is not initial.
            text =  text && |, | && usr-abtlg.
          endif.

          if usr-telnr is not initial.
            text =  text && |, Tel. | && usr-telnr.
          endif.

        else.
          text = afnam.
        endif.

* call up super method for all other attributes
      when others.
        text = super->s10standardname( attrname ).
    endcase.

  endmethod.

endclass.

class eban_manager definition inheriting from /s10/any.

  public section.

* project path
    constants:
       projectpath type string value 'sap/bc/bsp/s10/banf_demo'.

* table fields for list view, plus key fields
    data:
      search_frgzu          type eban-frgzu, " Freigabezustand
      search_txz01          type eban-txz01, " Kurztext
      search_rlwrt_minvalue type string. "Werteselektion

    data:
      tabeban type table of ref to eban_short.



    methods:
      list,
      release,
      reset_release,


* method to build up tabeban
      build_tabeban
        importing
          search_frgzu          type eban-frgzu " Freigabezustand
          search_txz01          type eban-txz01 " Kurztext
          search_rlwrt_minvalue type string "Werteselektion
        exporting
          tabeban               type table.

endclass.


class eban_manager implementation.

* display list screen.
  method list.
    s10nextscreen( 'list').
  endmethod.

  method release.

    data: rownumber type i.
    rownumber = s10contextinfo( )->rownumber.
    read table tabeban index rownumber assigning field-symbol(<c>).


    call function 'BAPI_REQUISITION_RELEASE'
      exporting
        number         = <c>->banfn
        rel_code       = '01'
        item           = <c>->bnfpo
        use_exceptions = 'X'    " Ausnahmen setzen und Fehlermeldungen ausgeben
      exceptions
        others         = 1.

    if sy-subrc <> 0.
      s10errormessage(
          msgid             =     sy-msgid
          msgno             =     sy-msgno
          par1              =     sy-msgv1
          par2              =     sy-msgv2 ).
    endif.


    call function 'BAPI_TRANSACTION_COMMIT'.

* update list display
    <c>->s10databaseread( ).

    s10infomessage(
      exporting
        text          =    s10localize( 'BANF_RELEASE_DONE' ) ).

  endmethod.

  method reset_release.

    data: rownumber type i.
    rownumber = s10contextinfo( )->rownumber.
    read table tabeban index rownumber assigning field-symbol(<c>).

    call function 'BAPI_REQUISITION_RESET_RELEASE'
      exporting
        number   = <c>->banfn
        rel_code = '01'
        item     = <c>->bnfpo
      exceptions
        others   = 1.
    if sy-subrc <> 0.
      s10errormessage(
          msgid             =     sy-msgid
          msgno             =     sy-msgno
          par1              =     sy-msgv1
         par2              =     sy-msgv2 ).
    endif.

    call function 'BAPI_TRANSACTION_COMMIT'.

* update list display
    <c>->s10databaseread( ).

    s10infomessage(
      exporting
        text          =    s10localize( 'BANF_RELEASE_RESET' ) ).

  endmethod.


* select database values and fill table tabeban
  method build_tabeban.

    data: condition type string,
          join      type string,
          search    type string.


    data: para          type tpara-paramid value 'KOS',
          ext_kostl(30) type c,
          user_kostl    type ebkn-kostl.

    get parameter id para field ext_kostl.

    if ext_kostl is initial..
      s10errormessage(  s10localize( 'KOS_MISSING' ) ).
    endif.

    s10userinput( exporting uservalue = conv string( ext_kostl )  changing result = user_kostl ).



    condition = |frgst ne ' ' and statu ne 'B' |.

    if search_frgzu is not initial.

      if condition is not initial.
        condition = condition && | and |.
      endif.

      case search_frgzu.
        when 'N'. condition = condition && |FRGZU NE 'X'|.
        when 'X'. condition = condition && |FRGZU EQ 'X'|.
      endcase.
    endif.

    if search_txz01 is not initial.
      search = to_upper( search_txz01 ).
      if condition is not initial.
        condition = condition && | and |.
      endif.
      condition = condition && | upper( TXZ01 ) like '%| && search && |%'|.
    endif.


    if  search_rlwrt_minvalue  is not initial.
      if condition is not initial.
        condition = condition && | and |.
      endif.
      case search_rlwrt_minvalue.
        when '1000'. condition = condition && |rlwrt LE 1000|.
        when '5000'. condition = condition && |rlwrt LE 5000|.
        when '5000+'. condition = condition && |rlwrt GE 5000|.
      endcase.
    endif.

*   Nur BANF mit zugeordneter Kostenstelle lesen (Tabelle EBKN)
    condition = condition &&
      | and exists ( select  *  from ebkn where banfn = eban~banfn and bnfpo =  eban~bnfpo and kostl = '| && user_kostl && |' ) |.


    data: myebkn    type table of ebkn,
          myebkn_wa type ebkn.




* read data
    s10databaseselect(
    exporting
      condition = condition
      orderby = 'badat,banfn,statu,matnr,menge,meins,rlwrt,waers,lfdat,txz01,frgzu,frgst'
      changing folder = tabeban ).

* Gesamtwert setzen, falls nicht gespeichert
    field-symbols: <b> type ref to eban_short.
    loop at tabeban assigning <b>.
      if <b>->rlwrt = 0.
        <b>->rlwrt = <b>->menge * <b>->preis.
      endif.
    endloop.



  endmethod.

endclass.


* main class for this application
class main definition inheriting from /s10/any.

  public section.

    data: my_eban_manager type ref to eban_manager.

    methods:
      logon.

endclass.

class main implementation.

* logon user
  method logon.

* set S10 license
    s10setlicense( 'Synactive GmbH demo license number=100 role=s10demo_role maxusers=10 signature=821.126.87.7' ).

*  create manager object
    create object my_eban_manager.

* set defaults
    my_eban_manager->search_frgzu = 'N'. "nicht freigegeben

* start list display
    my_eban_manager->list( ).

  endmethod.
endclass.